<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">
    <title>健康饮食推荐</title>
    <meta name="keywords" content="面向学习型的开源框架，简洁高效，杜绝过渡封装，展现技术本质本质">
    <meta name="description" content="面向学习型的开源框架，简洁高效，杜绝过渡封装，展现技术本质本质">
    <link rel="shortcut icon" href="favicon.ico">
    <link href="/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="/css/bootstrap-select.css?v=3.3.6" rel="stylesheet">
    <link href="/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="/css/plugins/toastr/toastr.min.css" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css?v=4.1.0" rel="stylesheet">
</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content ">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-content">
						<form class="form-horizontal m-t" id="signupForm" >
							<div class="form-group">
								<label class="col-sm-3 control-label">用户名:</label>
								<div class="col-sm-8">
									<input id="username" name="username" onblur="checkUsername(this);" class="form-control" type="text">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">密码:</label>
								<div class="col-sm-8">
									<input id="password" name="password" class="form-control" type="text">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">用户类型:</label>
								<div class="col-sm-8">
									<select data-placeholder="--选择类别--" name="userType" id="userType" data-live-search="true"
                                        class="form-control chosen-select selectpicker" tabindex="2" required>
                                    <option value="">--选择类别--</option>
                                    <option th:each="dict:${dicts}" th:value="${dict.code}" th:text="${dict.codeName}" th:selected="${dict.code=='3'}"></option>
                                </select>
								</div>
							</div>
							<div class="form-group">
								<div class="col-sm-8 col-sm-offset-3">
									<a id="submit" class="btn btn-primary">保存</a>
									<a type="button" class="btn btn-primary" th:href="@{/toUser}">返回</a>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	
<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4" th:src="@{/js/jquery.min.js?v=2.1.4}"></script>
<script src="/js/bootstrap.min.js?v=3.3.6" th:src="@{/js/bootstrap.min.js?v=3.3.6}"></script>
<script src="/js/bootstrap-select.js?v=3.3.6" th:src="@{/js/bootstrap-select.js?v1.6.3}"></script>

<!-- 自定义js -->
<script src="/js/content.js?v=1.0.0" th:src="@{/js/content.js?v=1.0.0}"></script>

<script type="text/javascript" src="/js/plugins/layer/laydate/laydate.js"></script>

<!-- jQuery Validation plugin javascript-->
<script src="/js/ajax-util.js"></script>
<script src="/js/plugins/validate/jquery.validate.min.js" th:src="@{/js/plugins/validate/jquery.validate.min.js}"></script>
<script src="/js/plugins/validate/messages_zh.min.js" th:src="@{/js/plugins/validate/messages_zh.min.js}"></script>
<script src="/js/plugins/layer/layer.min.js" th:src="@{/js/plugins/layer/layer.min.js}"></script>

<script type="text/javascript">

$(document).ready(function () {
	$(".selectpicker").selectpicker({
        'selectedText': 'cat'
    });
});

//检查用户名是否被占用
function checkUsername(obj){
	var username = $(obj).val().trim();
	if(username==null || username==''){
		layer.msg("请输入正确的用户名");
		return;
	}
	
	//提交至后台
	$.ajax({
		type: "POST",
		url: "/user/checkUsername",
		data: {username:$('#username').val()},
		success: function (r) {
			if (r.code != 0) {
				layer.msg(r.msg);
			}
		}
	});
}

//提交增加用户
$(document).ready(function(){
	$("#submit").on("click",function(){
		
		//先校验用户名
		var username = $('#username').val().trim();
		if(username==null || username==''){
			layer.msg("请输入正确的用户名");
			return;
		}
		
		//提交至后台
		$.ajax({
			type: "POST",
			url: "/user/checkUsername",
			data: {username:$('#username').val().trim()},
			success: function (r) {
				if (r.code != 0) {
					//用户名重复
					layer.msg(r.msg);
				}else{
					//检验密码
					var password = $("#password").val().trim();
					if(password==null || password==''){
						layer.msg("请输入密码");
						return;
					}
					
					//检验用户类型
					var userType = $("#userType").val().trim();
					if(userType==null || userType==''){
						layer.msg("请选择用户类型");
						return;
					}

					//若添加的是学生或教师，警告提示
					if(userType=="1" || userType=="2"){
					    if(!confirm("此功能仅适用于补全学生或教师登录用户异常缺失数据，确认要添加吗？")){
                            return;
						}

                        var warning =  prompt("我已知晓随意操作会造成异常数据，继续添加请输入：是","")
                        if(warning != "是"){
                            return;
                        }
					}

					//提交至后台
					$.ajax({
						data:$('#signupForm').serialize(),
						type:"POST",
						datatype:"json",
						url:"/user/add",
						success:function(r){
				            if (r.code == 0) {
				            	layer.msg("新增用户成功");
				            	window.location.href = '/toUser';
				            } else {
								layer.msg(r.msg);
				            }
						}
					});
				}
			}
		});
		
	})
	
});

</script>
</body>
</html>
